home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 11 / Mac Magazin and MacEasy Magazine CD - Issue 11.iso / Sharewarebibliothek / Entwickler / appe Windows 2.0 / main.c < prev    next >
C/C++ Source or Header  |  1995-06-11  |  8KB  |  290 lines

  1. // File "main.c" - 
  2.  
  3. #include <GestaltEqu.h>
  4. #include <TextServices.h>
  5. #include <Traps.h>
  6.  
  7. #include "main.h"
  8. #include "aevents.h"
  9. #include "filter.h"
  10. #include "floaters.h"
  11. #include "notify.h"
  12. #include "patches.h"
  13. #include "sample win.h"
  14.  
  15. // * ****************************************************************************** *
  16. // Global Declarations
  17.  
  18. GlobalsRec glob;
  19.  
  20. // * ****************************************************************************** *
  21. // * ****************************************************************************** *
  22.  
  23. void DoTest() {
  24.     short i;
  25.     long response;
  26.     
  27.     for(i=0; i<sizeof(glob); ((char *) &glob)[i++] = 0);
  28.     
  29.     // Simple Tests for compatibility
  30.     if (Gestalt(gestaltSystemVersion, &response) || (response < 0x0604) ||
  31.             Gestalt(gestaltTSMgrVersion, &response))
  32.         UserNotify(kNotifyStringList, kSys71NotifyString, TRUE);
  33.         
  34.     glob.bkgdOnly = ((** (short **) GetResource('SIZE', -1)) & 0x0400) ? TRUE : FALSE;
  35.     glob.hasColorQD = (! Gestalt(gestaltQuickdrawFeatures, &response) &&
  36.             (response & (1 << gestaltHasColor))) ? TRUE : FALSE;
  37.     glob.hasGDevices = (NGetTrapAddress(_GetDeviceList, ToolTrap) !=
  38.             NGetTrapAddress(_Unimplemented, ToolTrap)) ? TRUE : FALSE;
  39.     glob.hasDragMgr = (! Gestalt(gestaltDragMgrAttr, &response) &&
  40.             (response & (1 << gestaltDragMgrPresent))) ? TRUE : FALSE;
  41.  
  42.     // Inform the user how to find the "TSM Fix" if it isn't installed
  43.     if (Gestalt('TSM+', &response))
  44.         UserNotify(kNotifyStringList, kTSMFixNotifyString, FALSE);
  45.     }
  46.  
  47. // * ****************************************************************************** *
  48. // * ****************************************************************************** *
  49.  
  50. void DoInit() {
  51.     long saveA5;
  52.     
  53.     // 'appe' programs get 8k of Stack Space by default -- we need MORE! 
  54.     if (glob.bkgdOnly) SetApplLimit(GetApplLimit() - 16384);
  55.     MaxApplZone();
  56.     MoreMasters();
  57.  
  58.     // Basic Initialization 
  59.     InitGraf(&qd.thePort);
  60.     
  61.     // Finish our Initialization - but only if we are a foreground app. Apple
  62.     //   warns loudly that bkgd-only apps should not call InitWindows(), etc.
  63.     if (! glob.bkgdOnly) {
  64.         InitFonts();
  65.         InitWindows();
  66.         InitMenus();
  67.         TEInit();
  68.         InitDialogs(0);
  69.         InitCursor();
  70.         
  71.         InsertMenu(GetMenu(kAppleMenuID), 0);
  72.         AddResMenu(GetMHandle(kAppleMenuID), 'DRVR');
  73.         InsertMenu(GetMenu(kFileMenuID), 0);
  74.         InsertMenu(GetMenu(kEditMenuID), 0);
  75.         DisableItem(GetMHandle(kEditMenuID), 0);
  76.         
  77.         DrawMenuBar();
  78.         }
  79.       else {
  80.         // After reading the above, you know that you can't call any other toolbox
  81.         //   init stuff. However, FindServiceWindow() fails if the MenuList lomem 
  82.         //   global is NIL! This is a moderate (but not recommended) workaround.
  83.         InitFonts();
  84.         InitMenus();
  85.         }
  86.     
  87.     PatchNewWindow();
  88.     PatchExitToShell();
  89.  
  90.     // Save this information for later
  91.     GetCurrentProcess(&glob.myPSN);    
  92.         
  93.     // Install an event filter or die
  94.     SetA5(saveA5 = SetA5(0));
  95.     glob.filterProc =
  96.             (Ptr) InstallEventFilter((FilterHelperUPP) EventFilterHelper, (Ptr) saveA5);
  97.     if (! glob.filterProc)
  98.         UserNotify(kNotifyStringList, kJGNENotifyString, TRUE);
  99.     
  100.     InitHLEvents();
  101.     }
  102.  
  103. // * ****************************************************************************** *
  104. // * ****************************************************************************** *
  105.  
  106. void DoLoop() {
  107.     Boolean floaterEvent;
  108.     
  109.     while(! glob.quitting) {
  110.         // If there are no internal events pending, then we call WNE() with a
  111.         //   moderate delay. Otherwise, we want to make a fast response to any 
  112.         //   floater events, so we make sure the event filter will wake us up quickly.
  113.         if (glob.forwardedEvents.qHead) glob.theEvent.what = nullEvent;
  114.           else WaitNextEvent(everyEvent, &glob.theEvent, 150, 0);    // Sleep for 2.5 secs
  115.         
  116.         // Check the states of our floaters, then generate and handle any 
  117.         //   pending Update Events for floaters because the Event Mgr will
  118.         //   not generate them for us.
  119.         ShowHideFloater(0);
  120.         UpdateFloater(0);
  121.  
  122.         // If nothing to do, then check our private event queue to see if any
  123.         //   clicks or keydowns are pending for our floater windows.
  124.         floaterEvent = (glob.theEvent.what != nullEvent) ? FALSE : 
  125.                 GetFloaterEvent(&glob.theEvent);
  126.         
  127.         switch(glob.theEvent.what) {
  128.             case nullEvent:
  129.                 break;
  130.             case mouseDown: {
  131.                 short thePart;
  132.                 Rect bounds;
  133.                 WindowPtr whichWin, frontWin;
  134.                 
  135.                 if (floaterEvent) {
  136.                     thePart = FindServiceWindow(glob.theEvent.where, &whichWin);
  137.                     if (GetFrontServiceWindow(&frontWin)) frontWin = 0;
  138.                     }
  139.                   else {
  140.                     thePart = FindWindow(glob.theEvent.where, &whichWin);
  141.                     frontWin = FrontWindow();
  142.                     }
  143.  
  144.                 glob.theEvent.message = thePart;        
  145.                 switch(thePart) {
  146.                     case inMenuBar:
  147.                         DoMenuItem(MenuSelect(glob.theEvent.where));
  148.                         break;
  149.                         
  150.                     case inSysWindow:
  151.                         SystemClick(&glob.theEvent, whichWin);
  152.                         break;
  153.                         
  154.                     case inDrag:
  155.                         if (whichWin != frontWin) {
  156.                             SelectWindow(whichWin);
  157.                             UpdateFloater(whichWin);
  158.                             }
  159.                         bounds = (*GetGrayRgn())->rgnBBox;
  160.                         DragWindow(whichWin, glob.theEvent.where, &bounds);
  161.                         UpdateFloater(whichWin);
  162.                         break;
  163.                         
  164.                     case inContent:
  165.                     case inGrow:
  166.                     case inGoAway:
  167.                     case inZoomIn:
  168.                     case inZoomOut:
  169.                         if (whichWin != frontWin) {
  170.                             SelectWindow(whichWin);
  171.                             UpdateFloater(whichWin);
  172.                             }
  173.                         EventDispatchFloaters(&glob.theEvent, whichWin);
  174.                         break;
  175.                     }
  176.                 break;
  177.                 }
  178.             case keyDown:
  179.             case autoKey: {
  180.                 char theKey, theChar;
  181.                 
  182.                 theChar = glob.theEvent.message & charCodeMask;
  183.                 theKey = (glob.theEvent.message & keyCodeMask) >> 8;
  184.                 
  185.                 if (glob.theEvent.modifiers & cmdKey)
  186.                     DoMenuItem(MenuKey(glob.theEvent.message & charCodeMask));
  187.                   else EventDispatchFloaters(&glob.theEvent, 0);
  188.                 }
  189.                 break;
  190.                     
  191.             case updateEvt:
  192.                 if (GetOneFloater((WindowPtr) glob.theEvent.message, FALSE))
  193.                     EventDispatchFloaters(&glob.theEvent, (WindowPtr) glob.theEvent.message);
  194.                 break;
  195.             case activateEvt:
  196.                 break;
  197.             case kHighLevelEvent:
  198.                 AEProcessAppleEvent(&glob.theEvent);
  199.                 break;
  200.             }
  201.         
  202.         // WARNING: Dont set the cursor to arrow just because it is over a floater
  203.         //   window -- most foreground apps are not smart enough to recognize a
  204.         //   the TSM floaters, and the cursor may start to flicker.
  205.         if (! glob.bkgdOnly) SetCursor(&qd.arrow);
  206.         }
  207.     }
  208.  
  209. // * ****************************************************************************** *
  210. // * ****************************************************************************** *
  211.  
  212. void DoMenuItem(long theMenuAndItem) {
  213.     short theMenu, theItem;
  214.     Str63 theString;
  215.     
  216.     if (! theMenuAndItem) return;
  217.     
  218.     theMenu = (theMenuAndItem & 0xFFFF0000) >> 16;
  219.     theItem = theMenuAndItem & 0x0000FFFF;
  220.     
  221.     switch(theMenu) {
  222.         case kAppleMenuID:
  223.             if (theItem == kAppleMenuAboutItem) {
  224.                 // Block out the floaters while we display our about box... 
  225.                 //   then enable and update them after we have finished.
  226.                 glob.modalFloats = TRUE;
  227.                 ShowHideFloater(0);
  228.                 
  229.                 Alert(128, 0);
  230.                 
  231.                 glob.modalFloats = FALSE;
  232.                 ShowHideFloater(0);
  233.                 UpdateFloater(0);
  234.                 }
  235.               else {
  236.                 GetItem(GetMHandle(theMenu), theItem, theString);
  237.                 OpenDeskAcc(theString);
  238.                 }
  239.             break;
  240.             
  241.         case kFileMenuID:
  242.             switch(theItem) {
  243.                 case kFileMenuNewItem:
  244.                     NewSampleWindow();
  245.                     break;
  246.                 case kFileMenuQuitItem:
  247.                     glob.quitting = TRUE;
  248.                     break;
  249.                 }
  250.             break;
  251.             
  252.         case kEditMenuID:
  253.             break;
  254.  
  255.         default:
  256.             break;
  257.         }
  258.     HiliteMenu(0);
  259.     DrawMenuBar();
  260.     }
  261.  
  262. // * ****************************************************************************** *
  263. // * ****************************************************************************** *
  264.  
  265. void DoDispose() {
  266.     static Boolean done = FALSE;
  267.     
  268.     // Our ExitToShell() patch may cause re-entrancy problems... so we bracket
  269.     //   the functional calls by checking and setting a one-time flag.
  270.     if (! done) {
  271.         done = TRUE;
  272.         
  273.         CloseRemainingFloaters();
  274.         
  275.         ReleaseEventFilter(glob.filterProc);
  276.         glob.filterProc = 0;
  277.         }
  278.     }
  279.         
  280. // * ****************************************************************************** *
  281. // * ****************************************************************************** *
  282.  
  283. void main() {
  284.     DoTest();
  285.     DoInit();
  286.     DoLoop();
  287.     DoDispose();
  288.     }
  289.     
  290.